package com.lepu.app.fun.tool.jqtx.remind;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import java.io.Serializable;

import com.app.utils.DateUtil;

@SuppressWarnings("serial")
public class Alarm implements Serializable {

	public int alarmId; // 闹钟唯一标志
	
	public String uid;
	public long alarmTime; // 闹钟响起的时间
	public long setTime; // 设置的时间，用于区分时间相同的俩个闹钟
	//public String showTime; // 字符串形式的时间,用于回显
	public String note; // 备注回显

	//public long repeatTime; // 循环周期 ---- 不存数据库 ----!!!
	//public boolean isRepeat; // 是否循环 ---- 不存数据库 ----;不可为空!!!
	
	public int type;//什么提醒
	
	public static void setRemindNoDB(Alarm alarm, AlarmManager alarmManager,
			Context context) {
		setRemindUtil(alarm, alarmManager, context);
	}

	public static void setRemindUtil(Alarm alarm, AlarmManager alarmManager,
			Context context) {
		
		if(alarm == null)
		{
			return;
		}
		
//		String timeStr = String.valueOf(alarm.setTime);
//		alarm.alarmId = Integer.valueOf(timeStr.substring(timeStr.length() - 8,
//				timeStr.length()));

		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("alarm", alarm);
		intent.putExtras(bundle);

		intent.setAction("android.intent.action.ALARM_RECEIVER");
		intent.setClass(context, RemindReceiver.class);

		PendingIntent pi = PendingIntent.getBroadcast(context, alarm.alarmId,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);// PendingIntent.FLAG_UPDATE_CURRENT

		if (alarm.alarmTime > DateUtil.getNowTimeInMillis()) {
			alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.alarmTime, pi);
		}
	}

	public static void closeRemind(AlarmManager alarmManager, Context context,
			int alarmId) {
		Intent intent = new Intent();
		intent.setClass(context, RemindReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(context, alarmId, intent,
				0);
		alarmManager.cancel(pi);
	}

}
